package animation;

import de.pmg_grandma.R;
import de.pmg_grandma.R.anim;
import de.pmg_grandma.R.id;
import android.app.Activity;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class GrandmaAnimation
{
	private Activity parentActivity;
	public static GrandmaAnimation selfanimation;
	public static boolean isRunning = false;
	private ImageView animationImage;
	private static Animation anim;
	
	public static int wipeAnimation = 1; 
	public static int rotationAnimation = 2;
	
	public GrandmaAnimation(Activity parentActivity)
	{
		this.setParentActivity(parentActivity);
		selfanimation = this;
		makeAnimation(GrandmaAnimation.wipeAnimation);
	}
	
	private void makeAnimation(int animation)
	{
		animationImage = (ImageView) parentActivity.findViewById(R.id.grandMa);
		switch(animation)
		{
			case 1:
				anim = AnimationUtils.loadAnimation(parentActivity, R.anim.grandma_wipping);
				break;
			case 2:
				anim = AnimationUtils.loadAnimation(parentActivity, R.anim.grandma_rotation);
				break;
		}
	}

	public Activity getParentActivity()
	{
		return parentActivity;
	}

	public void setParentActivity(Activity parentActivity)
	{
		this.parentActivity = parentActivity;
	}
	
	public void startGrandmaAnimation()
	{
		isRunning = true;
		animationImage.startAnimation(anim);
	}
	
	public void stopGrandmaAnimation()
	{
		isRunning = false;
		animationImage.clearAnimation();
	}
	
	public static void startGranniRotation()
	{
		selfanimation.stopGrandmaAnimation();
		selfanimation.makeAnimation(GrandmaAnimation.rotationAnimation);
		selfanimation.startGrandmaAnimation();
		anim.setAnimationListener(new Animation.AnimationListener()
		{
		    @Override
		    public void onAnimationStart(Animation arg0) {
		    }           
		    @Override
		    public void onAnimationRepeat(Animation arg0) {
		    }           
		    @Override
		    public void onAnimationEnd(Animation arg0) {
		    	selfanimation.stopGrandmaAnimation();
				selfanimation.makeAnimation(GrandmaAnimation.wipeAnimation);
				selfanimation.startGrandmaAnimation();
		    }
		});
	}
}
